package com.zcf.gameController.gameUtils;
import com.google.gson.ExclusionStrategy;
import com.google.gson.FieldAttributes;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.zcf.gameController.WebSocketBull;
import com.zcf.pojo.Msg;
import com.zcf.pojo.RoomsParam;
import com.zcf.pojo.UserBean;
import org.springframework.beans.BeanUtils;
import javax.websocket.Session;
import java.util.*;
import java.util.stream.Collectors;
/**

 * @author ： jj
 * @date ：2019/11/20 9:31
 * @description :
 * @path : OpenCardsTimer.java.OpenCardsTimer
 */
public class OpenCardsTimer extends Thread{
	/**
	 * 牌型
	 */
	HandPattern px=new HandPattern();

	private  Integer timer;

	private RoomsParam myRoom;

	private WebSocketBull ws;
	private Map<String, Object> data = new HashMap<>();
	public OpenCardsTimer(Integer timer, RoomsParam myRoom, WebSocketBull ws){
		this.timer = timer;
		this.ws = ws;
		this.myRoom = myRoom;
	}
	@Override
	public void run() {
		while (timer >0 ) {
			if ( Thread.currentThread().isInterrupted() ) {
				System.out.println("线程终止");
				break;
			}
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			System.out.println("亮牌倒计时"+timer);
			timer --;
			myRoom.setTimer(timer);
			List<UserBean> userBeans =myRoom.getUsers().values().stream().filter(userBean->userBean.getIsReady().equals(1)).collect(Collectors.toList());
			Map<Integer, List<UserBean>> collect = userBeans.stream().collect(Collectors.groupingBy(UserBean::getOpenCards));
			int a=0;
			if (collect.get(1)!=null){
				a=collect.get(1).size();
			}
			System.out.println("在座人数:"+userBeans.size()+"亮牌人数:"+a);
			if (userBeans.size()==a){
				timer=0;
			}
			if(timer <= 0){
				double zSum=0.0;
				double xSum=0.0;
				data.clear();
				data.put("cards", myRoom.getCards());
				data.put("personal" , 1);
				data.put("isStart" , myRoom.getIsStart());
				Msg msg = new Msg(200,"亮牌", data,"openCard");
				try {
					ws.broadcast(msg, myRoom);
				} catch (Exception e) {
					e.printStackTrace();
				}
				myRoom.setIsStart(6);
				Map<Integer, List<Integer>> cardsMap = myRoom.getCards();
				Integer banker = myRoom.getIsUp().intValue();
				Integer up = myRoom.getPostions().indexOf(banker);
				List<Integer> bankerCards = cardsMap.get(up);
				zSum = px.isTs(bankerCards);
				if (zSum ==-1){
					zSum = px.sum(bankerCards)+10;
				}
				for (Integer position:cardsMap.keySet()){
					if (position.equals(banker)){
						continue;
					}
					xSum = px.isTs(cardsMap.get(position));
					if (xSum ==-1){
						xSum=px.sum(cardsMap.get(position))+10;
					}
					if (zSum<10&&xSum<10){
						if (zSum%10<xSum%10){
							myRoom.getBunko().put(position,1);
						}else {
							myRoom.getBunko().put(position,0);
						}
					}
					if (zSum>=10&&xSum<10){
						myRoom.getBunko().put(position,1);
					}
					if (zSum<10&&xSum>=10){
						myRoom.getBunko().put(position,0);
					}
					if (zSum>=10&&xSum>=10){
						if (zSum%10<xSum%10){
							myRoom.getBunko().put(position,1);
						}else if(zSum%10==xSum%10) {
							int zMax;
							int xMax;

							List<Integer> bcd = cardsMap.get(up);
							int zTbrand0 = HandPattern.tbrands[bcd.get(0)];
							int zTbrand1 = HandPattern.tbrands[bcd.get(1)];
							List<Integer> list = cardsMap.get(position);
							int zTbrand2 = HandPattern.tbrands[list.get(0)];
							int zTbrand3 = HandPattern.tbrands[list.get(1)];

							if (zTbrand0>zTbrand1){
								zMax=zTbrand0;
							}else {
								zMax = zTbrand1;
							}
							if (zTbrand2>zTbrand3){
								xMax=zTbrand2;
							}else {
								xMax = zTbrand3;
							}
							if (zMax<xMax){
								myRoom.getBunko().put(position,1);
							}else {
							    myRoom.getBunko().put(position, 0);
							}
						}else {
							myRoom.getBunko().put(position,0);
						}
					}
				}
				UserBean zUser=myRoom.getUsers().get(myRoom.getIsUp()+"");
				for (String userid:myRoom.getUsers().keySet()){
					if (userid.equals(myRoom.getIsUp()+"")){
						continue;
					}
					UserBean userBean = myRoom.getUsers().get(userid);
					for (Integer positions:userBean.getBetList().keySet()){
						if (positions.equals(zUser.getPosition())){
							continue;
						}
						Integer bunko = myRoom.getBunko().get(positions);
						Integer betMoney = userBean.getBetList().get(positions);

						if (bunko==1){
							userBean.setMoney(userBean.getMoney()+betMoney*2);
							userBean.setSumWinnumber(userBean.getSumWinnumber()+betMoney);
							zUser.setSumWinnumber(zUser.getSumWinnumber()-betMoney);
						}else {
							userBean.setSumWinnumber(userBean.getSumWinnumber()-betMoney);
							zUser.setSumWinnumber(zUser.getSumWinnumber()+betMoney);
						}
					}
				}
				if (zUser.getSumWinnumber()>0){
					zUser.setSumWinnumber(zUser.getSumWinnumber() - (int)(zUser.getSumWinnumber()*myRoom.getPumping()));
				}
				zUser.setMoney(zUser.getMoney()+zUser.getSumWinnumber());
				try {
					Thread.sleep(3000);
				}catch (Exception e){
					e.printStackTrace();
				}
				data.clear();
				data.put("users",userBeans);
				data.put("betMoney",myRoom.getBetMoeny());
				data.put("bunko",myRoom.getBunko());
				data.put("cards",myRoom.getCards());
				data.put("isStart" , myRoom.getIsStart());
				data.put("isUp",myRoom.getIsUp());
				data.put("positions",myRoom.getPostions());
				Msg ms = new Msg(200,"结算", data,"balance");
				try {
					ws.broadcast(ms, myRoom);
				} catch (Exception e) {
					e.printStackTrace();
				}
				for (UserBean us:myRoom.getUsers().values()){
					data.clear();
					data.put("money",us.getMoney());
					data.put("sumWinnumber",us.getSumWinnumber());
					Msg ms1 = new Msg(200,"结算", data,"userBalance");
					try {
						ws.broadcastMe(ms1, us.getSession(),us);
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
				List<UserBean> users = new ArrayList<>();
				for (UserBean userBean:myRoom.getUsers().values()){
					UserBean u2=new UserBean();
					BeanUtils.copyProperties(userBean,u2);
					users.add(u2);
				}
				AccountThread accountThread=new AccountThread(users,myRoom.getRoomGrade(),myRoom.getRoomName(),myRoom.getPumping(),myRoom.getGametype());
				accountThread.start();
				System.out.println("--------------------------开始下一轮-------------------------------");
				myRoom.setIsStart(0);
				myRoom.clear();
				List<UserBean> mRoom=new ArrayList<>(myRoom.getUsers().values());
				for (UserBean ur:mRoom){
					if (ur.getType()==3){
						if (myRoom.getPostions().contains(ur.getId().intValue())){
							int i1 = myRoom.getPostions().indexOf(ur.getId().intValue());
							myRoom.getPostions().set(i1,0);
						}
						ur.setPosition(-1);
						myRoom.getUsers().remove(ur.getId().toString());
					}
				}
				data.clear();
				UserBean userBean = myRoom.getUserBean(myRoom.getIsUp());
				if (userBean!=null){
					if (userBean.getIsOwner()==4){
						data.put("isOwner",userBean.getId());
					}else {
						data.put("isOwner",0);
					}
				}else {
					data.put("isOwner",0);
				}
				myRoom.setIsUp(0L);
				try {
					List<UserBean> userBeanList =myRoom.getUsers().values().stream().filter(userBean1->userBean1.getType().equals(1)).collect(Collectors.toList());
					data.put("isStart" , myRoom.getIsStart());
					data.put("users" , userBeanList);
					data.put("positions" , myRoom.getPostions());
					data.put("isStart" , myRoom.getIsStart());
					Msg m=new Msg(200,"开启下一轮",data,"round");
					ws.broadcast(m,myRoom);
				} catch (Exception e) {
					e.printStackTrace();
				}

			}
		}
	}
}
